package com.ironman.compute;

import cn.hutool.core.collection.ListUtil;
import cn.hutool.core.date.DatePattern;
import cn.hutool.core.date.DateUtil;
import com.ironman.common.OriginalSimpleData;
import com.ironman.common.entity.HeadingData;
import com.ironman.common.util.AccuracyUtil;
import com.ironman.event.topic.dami.OriginalTopic;
import lombok.extern.slf4j.Slf4j;
import org.noear.dami.Dami;

import java.util.Date;
import java.util.List;
import java.util.function.BiConsumer;

/**
 * 航向计算
 */
@Slf4j
public class HeadingVectorComputer extends BaseVectorComputer<HeadingData> {

    public HeadingVectorComputer(Integer windowSize) {
        super(windowSize);
    }

    public void init(BiConsumer<Date, Double> headingConsumer) {
        Dami.<OriginalSimpleData, Void>bus().listen(OriginalTopic.ORIGINAL_TOPIC + GPS, payload -> {
            OriginalSimpleData originalSimpleData = payload.getContent();
            Date ts = originalSimpleData.getTs();
            String data = originalSimpleData.getData();
            String[] split = data.split(",");
            computeWindowData(new HeadingData(ts, Integer.parseInt(split[4]), Float.parseFloat(split[5])), headingConsumer);
        });
    }

    public void computeWindowData(HeadingData headingData, BiConsumer<Date, Double> headingConsumer) {
        windowData(headingData, headingConsumer);
    }

    public static void main(String[] args) {
        HeadingVectorComputer headingComputer = new HeadingVectorComputer(3);

        List<HeadingData> headingDataList = ListUtil.of(
                new HeadingData(DateUtil.parse("2024-12-15 12:00:00", DatePattern.NORM_DATETIME_PATTERN), 112, 0f),
                new HeadingData(DateUtil.parse("2024-12-15 12:00:01", DatePattern.NORM_DATETIME_PATTERN), 305, 0f),
                new HeadingData(DateUtil.parse("2024-12-15 12:00:02", DatePattern.NORM_DATETIME_PATTERN), 148, 0f),
                new HeadingData(DateUtil.parse("2024-12-15 12:00:03", DatePattern.NORM_DATETIME_PATTERN), 268, 0f),
                new HeadingData(DateUtil.parse("2024-12-15 12:00:04", DatePattern.NORM_DATETIME_PATTERN), 59, 0f),
                new HeadingData(DateUtil.parse("2024-12-15 12:00:05", DatePattern.NORM_DATETIME_PATTERN), 6, 0f),
                new HeadingData(DateUtil.parse("2024-12-15 12:00:06", DatePattern.NORM_DATETIME_PATTERN), 11, 0f),
                new HeadingData(DateUtil.parse("2024-12-15 12:00:07", DatePattern.NORM_DATETIME_PATTERN), 114, 0f),
                new HeadingData(DateUtil.parse("2024-12-15 12:00:08", DatePattern.NORM_DATETIME_PATTERN), 28, 0f),
                new HeadingData(DateUtil.parse("2024-12-15 12:00:09", DatePattern.NORM_DATETIME_PATTERN), 338, 0f)
        );
        for (HeadingData headingData : headingDataList) {
            headingComputer.computeWindowData(headingData, new BiConsumer<Date, Double>() {
                @Override
                public void accept(Date ts, Double average) {
                    log.info("{}=>当前平均航向{}°", ts, AccuracyUtil.downDirectionInteger(average));
                }
            });
        }

    }
}
